package com.shengdong.water.code.adapter

import com.shengdong.water.R
import com.shengdong.water.code.base.BaseRecyclerAdapter
import com.shengdong.water.code.bean.MapOutletBean
import com.shengdong.water.code.bean.OutletTable
import com.shengdong.water.databinding.ItemMapOutletBinding


class MapOutletAdapter : BaseRecyclerAdapter<OutletTable, ItemMapOutletBinding>() {
    private var selectedPosition = -1
    private var onItemSelectedListener: ((OutletTable) -> Unit)? = null

    override fun getLayoutId() = R.layout.item_map_outlet
    override fun bindData(binding: ItemMapOutletBinding, item: OutletTable, position: Int) {

        binding.item = item
//        binding.itemTable1.text = item.deptName
        binding.itemTable2.text = item.totalNum.toString()
        binding.itemTable3.text = item.completedNum.toString()


        // 设置选中状态的背景
        if (position == selectedPosition) {
            binding.root.setBackgroundColor(binding.root.context.getColor(R.color.buttonColor))
            binding.itemTable1.setTextColor(binding.root.context.getColor(R.color.white))
            binding.itemTable2.setTextColor(binding.root.context.getColor(R.color.white))
            binding.itemTable3.setTextColor(binding.root.context.getColor(R.color.white))
        } else {
            binding.root.setBackgroundColor(android.graphics.Color.TRANSPARENT)
            binding.itemTable1.setTextColor(binding.root.context.getColor(R.color.textColor))
            binding.itemTable2.setTextColor(binding.root.context.getColor(R.color.textColor))
            binding.itemTable3.setTextColor(binding.root.context.getColor(R.color.textColor))
        }
        binding.executePendingBindings()
    }

    init {
        // 在初始化时设置基类的点击监听
        setOnItemClickListener { item, position ->
            val oldPosition = selectedPosition
            selectedPosition = position

            // 刷新旧选中项和新选中项
            notifyItemChanged(oldPosition)
            notifyItemChanged(position)

            onItemSelectedListener?.invoke(item)
        }
    }

    fun setOnItemSelectedListener(listener: (OutletTable) -> Unit) {
        onItemSelectedListener = listener
    }


    fun clearSelection() {
        val oldPosition = selectedPosition
        selectedPosition = -1
        notifyItemChanged(oldPosition)
    }
}